package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.sys.dao.DaoUser;
import com.cy.pj.sys.pojo.HouseUser;
import com.cy.pj.sys.pojo.SysUser;
import com.cy.pj.sys.service.ServiceUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.ServerException;
import java.util.List;
import java.util.UUID;

@Service
public class ServiceUserImpl implements ServiceUser {
    @Autowired
    private DaoUser daoUser;

    //根据客户username查询客户信息
    @Override
    public List<SysUser> findUserByUserName(String username) {
        List<SysUser> userByUserName = daoUser.findUserByUserName(username);
        return userByUserName;
    }
    //根据客户username修改客户信息
    @Override
    public int updateUser(SysUser sysUser) {
        int i = daoUser.updateUser(sysUser);
        return i;
    }
    //注册
    @Override
    public int insertUser(SysUser users) {
        //产生一个随机字符串作为加密盐使用
        String salt = UUID.randomUUID().toString();
        //对密码进行MD5盐值加密（MD5特点：不可逆，相同内容加密结果也相同）
        SimpleHash sh = new SimpleHash("MD5", users.getPassword(), salt, 1);
        String hashedPassword = sh.toHex();
        users.setSalt(salt);
        users.setPassword(hashedPassword);
        int rows = daoUser.insertUsers(users);
        return rows;
    }
    //登录
    @Override
    public SysUser selectUser(String username, String password) {

        SysUser sysUser = daoUser.selectUser(username, password);
        if(sysUser==null){
            try {
                throw new ServerException("输入有误，请核对后重新输入");
            } catch (ServerException e) {
                e.printStackTrace();
            }
        }
        return sysUser;
    }

    //修改密码
    @Override
    public int updatePassword(String password,
                              String newPassword,String cfgPassword) {
        //1.判定新密码与密码确认是否相同
        if(StringUtil.isEmpty(newPassword))
            throw new IllegalArgumentException("新密码不能为空");
        if(StringUtil.isEmpty(cfgPassword))
            throw new IllegalArgumentException("确认密码不能为空");
        if(!newPassword.equals(cfgPassword))
            throw new IllegalArgumentException("两次输入的密码不相等");
        //2、判断原密码是否正确
        if(StringUtil.isEmpty(password))
            throw new IllegalArgumentException("原密码不能为空");
        //获取登录名
        SysUser user = (SysUser)
                SecurityUtils.getSubject().getPrincipal();
        SimpleHash sh = new SimpleHash(
                "MD5",password,user.getSalt(),1);
        if(!user.getPassword().equals(sh.toHex()))
            throw new IllegalArgumentException("原密码不正确");
        //3、对新密码进行加密
        String salt = UUID.randomUUID().toString();
        sh = new SimpleHash("MD5", newPassword, salt, 1);
        //4、将新密码加密以后的结果更新到数据库
        int rows = daoUser.updatePassword(sh.toHex(), salt, user.getId());
        if(rows==0)
            throw new ServiceException("修改失败");
        return rows;
    }


}
